package com.bawei.mvvmzonghe.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.mvvmzonghe.R;
import com.bawei.mvvmzonghe.databinding.ItemCateegoryLeftBinding;
import com.bawei.mvvmzonghe.databinding.ItemCategoryTopBinding;
import com.bawei.mvvmzonghe.entity.CateGoryEntity;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/9/14 10:07
 * @Description : CateGroryAdapter 一级分类适配器
 */
public class CateGoryTopAdapter extends RecyclerView.Adapter<CateGoryTopAdapter.CateGoryViewHolder> {
    private List<CateGoryEntity> mCateGoryEntityList;

    public CateGoryTopAdapter(List<CateGoryEntity> cateGoryEntityList) {
        mCateGoryEntityList = cateGoryEntityList;
    }

    @NonNull
    @Override
    public CateGoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCategoryTopBinding itemCategoryTopBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_category_top, parent, false);
        return new CateGoryViewHolder(itemCategoryTopBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull CateGoryViewHolder holder, int position) {
        holder.mItemCategoryTopBinding.setCategory(mCateGoryEntityList.get(position));
        //点击事件通过接口回调给activity
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemClickLister != null){
                    mItemClickLister.onItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mCateGoryEntityList.size();
    }

    class CateGoryViewHolder extends RecyclerView.ViewHolder{
       ItemCategoryTopBinding mItemCategoryTopBinding;

        public CateGoryViewHolder( ItemCategoryTopBinding itemCategoryTopBinding) {
            super(itemCategoryTopBinding.getRoot());
            mItemCategoryTopBinding = itemCategoryTopBinding;
        }
    }


    private CateGoryAdapter.ItemClickLister mItemClickLister;

    public void setItemClickLister(CateGoryAdapter.ItemClickLister itemClickLister) {
        mItemClickLister = itemClickLister;
    }

    //接口回调
    public interface ItemClickLister{
        void onItemClick(View view,int position);
    }

}
